package ${package}.${moduleName}.service.impl;

import java.io.Serializable;
import java.util.List;
import ${package}.${moduleName}.mapper.MyBaseMapper;
import com.github.pagehelper.PageHelper;
import ${package}.${moduleName}.service.BaseService;
import com.qigio.api.util.Page;

import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

public abstract class BaseServiceImpl<M extends MyBaseMapper<T>, T> implements BaseService<T> {

    @Getter
	@Autowired
	protected M mapper;

	@Override
	public Page<T> page(Example example, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		return new Page<T>(mapper.selectByExample(example));
	}

	@Override
	public List<T> list(Example example) {
		return mapper.selectByExample(example);
	}


    @Override
    public T getByPrimaryKey(Serializable id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean removeByPrimaryKey(Serializable id) {
        int count = mapper.deleteByPrimaryKey(id);
        return 1 == count;
    }

    @Override
    public boolean insert(T entity) {
        int count = mapper.insert(entity);
        return 1 == count;
    }

    @Override
    public boolean updateByPrimaryKey(T entity) {
        int count = mapper.updateByPrimaryKey(entity);
        return 1 == count;
    }

    @Override
    public boolean updateByPrimaryKeySelective(T entity) {
        int count = mapper.updateByPrimaryKeySelective(entity);
        return 1 == count;
    }

    @Override
    public boolean updateByExample(T entity, Example example) {
        int count = mapper.updateByExample(entity, example);
        return count > 0;
    }

    @Override
    public boolean updateByExampleSelective(T entity, Example example) {
        int count = mapper.updateByExampleSelective(entity, example);
        return count > 0;
    }
}